#coding=utf8

import os
from base.server_auto_start import Server
from util.configer_generator import configer
from util.path_generator import filePath
import re

#adb shell pm list packages

class Relation:#通过包名使设备与ini中的capacity的capacity匹配

    def __init__(self):
        self.devices=Server.find_Devices()
        self.package_section_list=[]
        self.apk_section_list=[]
        self.device_package={}
        self.device_apk_appname={}
        self.device_capacity={}
        self.apk_device_capacity={}
        self.device_webview={}
        self.device_apk_package={}
        self.apk_package={}
        self.section_List()
        self.get_Webview_Version()
        self.init_apk_dic()
        # self.init_apk_package()

        flag = configer.get_str("if_install_auto", "flag")
        if flag == "apk":
            for device in self.devices:
                self.device_apk_package[device]=self.apk_package[self.device_apk_appname[device]]

        for i in self.package_section_list:
            package=configer.get_str(i,"package")
            if self.devices!=None:
                for j in self.devices:
                    if self.find_Package(j,package):
                        self.device_capacity[j]=i
                        self.device_package[j]=package


    @staticmethod
    def find_Package(device,packagename):#查看安装包名
        #adb -s 7ccaa33 shell pm list packages | findstr com.houhan.yjqz
        try:
            ret=os.popen("adb -s %s shell pm list packages | findstr %s"%(device,packagename)).read()
            # print("adb -s %s shell pm list packages | findstr %s"%(device,packagename))
            ret_list=ret.strip().split(":")
            if ret_list[1]==packagename:
                return True
            else:
                return False
        except:
            return None

    def section_List(self):
        for i in configer.sections:
            if "capacity" in i:
                self.package_section_list.append(i)
            if "apk" in i:
                self.apk_section_list.append(i)

    def get_Webview_Version(self):#获取连接设备与其webview版本的字典
        #adb shell dumpsys package com.android.webview | findstr versionName
        chrome_list=os.listdir(filePath.path_chromedriver)
        # temp_chrome_list=[]
        # for i in range(len(chrome_list)):
        #     temp=chrome_list[i].split("-")
        #     temp[0]=int(temp[0])
        #     temp[1]=int(temp[1])
        #     temp_chrome_list.append(temp)
        # print("temp_chrome_list:"+str(temp_chrome_list))
        if self.devices!=None:
            for i in self.devices:
                find_webview_package=os.popen("adb -s %s shell pm list packages | findstr android.webview"%i).read()
                webview_package=find_webview_package.strip().split(":")[1]
                ret=os.popen("adb -s %s shell dumpsys package %s | findstr versionName"%(i,webview_package)).read()
                ret_list=ret.strip().split("=")
                ret_v=re.search(r"\d+\b",ret_list[1]).group(0)
                print()
                ret_v=int(ret_v)

                # for j in range(len(temp_chrome_list)):
                #     if ret_v>=temp_chrome_list[j][0] and ret_v<=temp_chrome_list[j][1]:
                #         ret_v=chrome_list[j]
                #         break
                # else:
                #     return None
                ret_v = chrome_list[0]
                self.device_webview[i]=ret_v

    def init_apk_dic(self):
        temp_apk_dic = {}
        for i in self.apk_section_list:
            apk_name=configer.get_str(i, "apk_name")
            temp_apk_dic[apk_name]=i
        temp_apk_list=list(temp_apk_dic.keys())
        if len(temp_apk_list)<len(self.devices):
            length=len(temp_apk_list)
        else:
            length=len(self.devices)
        for j in range(length):
            device=self.devices[j]
            tem_apk_name=temp_apk_list[j]
            self.apk_device_capacity[device]=temp_apk_dic[tem_apk_name]
            self.device_apk_appname[device]=tem_apk_name


    def init_apk_package(self):
        apk_list=os.listdir(filePath.path_apk)
        for i in apk_list:
            self.get_package_apk(i)


    def get_package_apk(self,apk_name):
        app_path=filePath.path_apk+apk_name
        ret = os.popen("aapt d badging %s | findstr package" % app_path).read()
        package=re.search(r"name='(.+?)'",ret).group(1)
        self.apk_package[apk_name]=package









if __name__=="__main__":
    r=Relation()
    print(r.device_webview)
    print(r.device_capacity)
    print(r.device_package)

